/**
 * Created by tandelin on 2017/8/22.
 */
$('body').hide();
//点击展示所有热评新闻
var request = 'eyJhbGciOiJIUzI1NiJ9.eyJleHAiOjE1MDE5ODkyNjYsInVzZXJJZCI6IjE1d2F5NXh4bmRjM29ld3VvZ2N6In0.K41L7hgqsPR-GPSN1jNDd4WWJ2BlEifAHSYvO3teAHY';

function getUrlParam(id){
    var reg = new RegExp("(^|&)" + id + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null){
        return decodeURI(r[2]);
    }
    return null;
}

var openImg = new Array(),
    indx = 0,
    param = {
        postId: getUrlParam('id')
    };

function ajax(url, param) {
    return $.ajax({
        url: url,
        data: param || {},
        type: 'POST',
        dataType: "json",
        beforeSend: function (request) {
            request.setRequestHeader("Authorization", localStorage.token)
        }
    });
};

/*
 * 获取新闻列表信息
 */
ajax("../news/posts/getPostsDtlById",param).done(function (resp){
    var data = resp,
        headerHtml = $('head').html(),
        newsTime = new Date(data.data.showDate*1000),
        showTime = [newsTime.getFullYear(),(newsTime.getMonth()+1)<10? '0'+ (newsTime.getMonth()+1):(newsTime.getMonth()+1),newsTime.getDate()<10? '0'+newsTime.getDate():newsTime.getDate(),newsTime.getHours()<10? '0'+newsTime.getHours():newsTime.getHours(),newsTime.getMinutes()<10?'0'+newsTime.getMinutes():newsTime.getMinutes(),newsTime.getSeconds()<10? '0'+newsTime.getSeconds():newsTime.getSeconds()],
        time = showTime[0]+ '-' +showTime[1]+ '-'+showTime[2]+ ' '+showTime[3]+ ':' +showTime[4]+ ':' +showTime[5];
    //$('.firstTitle').text(data.data.postTitle);
    //$('.newsTime').text(time);
    //$('.newsFrom').text(data.data.authors.name);
    //$('.newsHeaderImg img').attr('src',data.data.authors.img);
    $('.content').html(data.data.postContent);
    if($('p').html() === ""){
        $(this).hide();
    };
    // $('head').html(headerHtml + data.data.header);
    $.each($('.content img'),function (index,item) {
        $(this)[0].onload = function(){
            if(this.width > 100){
                console.log(this.width);
                $(this).addClass('showImgs')
            }
        };
        if($(this).css('display') != 'none'&&$(this).parent().css('display') != 'none'){
            $(this).data('ind',indx);
            openImg.push(item.src);
            //console.log(indx);
            console.log($(this).data('ind'));
            indx ++;
        }
    });
    $('body').show();
    console.log(openImg);
});

/**
 * 图片点击事件
 */
$('.content').on('click','img',function () {
    console.log(openImg);
    console.log($(this).data('ind'));
    console.log($(this).attr('src'));
    window.imagelistenner.openImages($(this).data('ind'),openImg );
});
